package com.mydemo.liteflow;

import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.Config;
import com.yomahub.liteflow.parser.JsonFlowParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;

/**
 * @author liming1
 */
@Slf4j
public class ApolloJsonFlowParser extends JsonFlowParser {

    private static final String LITE_FLOW_CHAIN_CONF_KEY = "lite.flow.chain.conf";

    @Autowired
    private Config config;

//    @Override
//    public void parseMain(List<String> pathList) throws Exception {
////        String flowConf = config.getProperty(LITE_FLOW_CHAIN_CONF_KEY, "{}");
////        super.parse(flowConf);
////
////        Set keySet = new HashSet<>();
////        keySet.add(LITE_FLOW_CHAIN_CONF_KEY);
////        config.addChangeListener(changeEvent -> {
////            log.info("我的值=" + flowConf);
////            try {
////                parse(flowConf);
////            } catch (Exception e) {
////                log.info("解析liteFlow异常", e);
////            }
////        }, keySet);
//    }

    @Value("${lite.flow.chain.conf:{}}")
    private String liteFlowChainConf;

    @Override
    public void parseMain(List<String> pathList) throws Exception {
        String conf = liteFlowChainConf;
        if (StringUtils.isBlank(conf)) {
            conf = new JSONObject().toJSONString();
        }
        super.parse(conf);
    }
}
